package sohsite.fachada;

import java.util.Collection;

import org.hibernate.Transaction;

import sohsite.excecao.EntradaInexistenteException;
import sohsite.excecao.ParametroInvalidoException;
import sohsite.reserva.CadastroReserva;
import sohsite.reserva.IRepositorioReserva;
import sohsite.reserva.Reserva;
import sohsite.util.generico.ControladorGenerico;

public class ControladorReserva extends ControladorGenerico{
	
	private CadastroReserva cadastroReserva;
	
	public ControladorReserva(IRepositorioReserva repositorioReserva){
		super();
		this.cadastroReserva = new CadastroReserva(repositorioReserva);
	}

	public void inserirReserva(Reserva reserva){
		Transaction transacao = iniciarTransacao();
		try {
			this.cadastroReserva.inserir(reserva);
			
		} catch (ParametroInvalidoException e) {
			e.printStackTrace();
		}
		transacao.commit();
	}
	
	public void deletarReserva(Reserva reserva){
		Transaction transacao = iniciarTransacao();
		try {
			this.cadastroReserva.deletar(reserva);
			
		} catch (ParametroInvalidoException e) {
			e.printStackTrace();
		}
		transacao.commit();
	}
	
	public void deletarReserva(int idReserva) throws EntradaInexistenteException{
		Transaction transacao = iniciarTransacao();
		try {
			this.cadastroReserva.deletar(idReserva);
			
		} catch (ParametroInvalidoException e) {
			e.printStackTrace();
		}
		transacao.commit();
	}
	
	public void atualizarReserva(Reserva reserva){
		Transaction transacao = iniciarTransacao();
		try {
			this.cadastroReserva.atualizar(reserva);
			
		} catch (ParametroInvalidoException e) {
			e.printStackTrace();
		}
		transacao.commit();
	}
	
	public Reserva consultarReserva(Integer idReserva) throws EntradaInexistenteException{
		Transaction transacao = iniciarTransacao();
		Reserva reserva = null;		
		
		try {
			reserva = this.cadastroReserva.consultar(idReserva);
			
		} catch (ParametroInvalidoException e) {
			e.printStackTrace();
		}
		transacao.commit();
		
		return reserva;
	}
	
	public Collection<Reserva> consultarTodasReservas() throws EntradaInexistenteException{
		Transaction transacao = iniciarTransacao();
		Collection<Reserva> reservas = null;		
		
		reservas = this.cadastroReserva.consultarTodos();

		transacao.commit();
		
		return reservas;
	}
}
